﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using adddependentscodefile;
using addqualificationcodefile;
using addemployeerecordcodefile;
using MetriCam;
using System.Drawing.Imaging;



namespace Employee_Management_System
{
    
    public partial class UserForm : Form
    {
        LoginForm formLogin;
        adddependents adddeps = new adddependents();
        addqualification addqual = new addqualification();
        addemployee ae = new addemployee();
        private WebCam camera;
        
        
        string imgpth = Environment.CurrentDirectory;
        
        public UserForm(LoginForm parent)
        {
            formLogin = parent;

            InitializeComponent();
            
            camera = new WebCam();
            /*btnlogout.Click += new EventHandler(btnlogout_Click);
            btnadddependents.Click += new EventHandler(btnadddependents_Click);
            btnaddemployee.Click += new EventHandler(btnsave_Click);
            btnaddcareer.Click += new EventHandler(btnaddcareer_Click);*/
            this.Load += new EventHandler(UserForm_Load);
            blogout.Click += new EventHandler(btnlogout_Click);
            
            badddependents.Click += new EventHandler(btnadddependents_Click);
            adddeps.addcompleted += new EventHandler(adddeps_addcompleted);

            baddcareer.Click += new EventHandler(btnaddcareer_Click);
            addqual.addcompleted += new EventHandler(addqual_addcompleted);

            baddemployee.Click += new EventHandler(btnaddemployee_Click);
            ae.addcompleted += new EventHandler(ae_addcompleted);

            
            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
            bcapture.Click += new EventHandler(btncapture_Click);
            

        }

        void btncapture_Click(object sender, EventArgs e)
        {
           
            if (!backgroundWorker1.CancellationPending)
            {
                picboxemp.Image = camera.GetBitmap();
                backgroundWorker1.CancelAsync();
            }
        }



        void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            camera.Disconnect();
            
        }

        void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            while (!backgroundWorker1.CancellationPending)
            {
                camera.Update();
                picboxemp.Image = camera.CalcBitmap();
            }
            
        }


        //adding employee records
        void ae_addcompleted(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                EventHandler eh = new EventHandler(ae_addcompleted);
                this.Invoke(eh);
                return;
            }
            ae.donCommit();
            MessageBox.Show("Employee Successfully Added!");
            settext();
            camera.Connect();
            backgroundWorker1.RunWorkerAsync();
        }
        void btnaddemployee_Click(object sender, EventArgs e)
        {
            imgpth = imgpth + "\\Images\\";

            picboxemp.Image.Save(@"" + imgpth + txtempnum.Text + ".jpg", ImageFormat.Jpeg);

            string name = txtsurname.Text+", "+txtfirstname.Text+" y "+txtmiddlename.Text;
            string presadd=txtpresadd1.Text+" "+txtpresadd2.Text+" "+txtpresadd3.Text+", "+txtpresadd4.Text;
            string permadd = txtpermadd1.Text+" "+txtpermadd2.Text+" "+txtpermadd3.Text+", "+txtpermadd4.Text;

            string marital ="";

            if(radbtnsingle.Checked==true)
            {
                marital=radbtnsingle.Text;
            }
            if(radbtnmarried.Checked==true)
            {
                marital=radbtnmarried.Text;
            }
            if(radbtnwidow.Checked==true)
            {
                marital=radbtnwidow.Text;
            }
            if(radbtnseparated.Checked==true)
            {
                marital=radbtnseparated.Text;
            }
            string bday = combomonth.Text+" "+comboday.Text+", "+comboyear.Text;
            string spouse=txtspouse2.Text+" "+txtspouse3.Text+" "+txtspouse1.Text;
            ae.asyncaddprofile(txtempnum.Text,combostatus.Text,name, txtdept.Text,txtpos.Text,txtgender.Text,txtempcontact.Text,
                presadd,permadd,txtempheight.Text,txtempweight.Text,marital,bday,txtage.Text,txttin.Text,txtsss.Text,txtphilhealth.Text,
                txtpagibig.Text,comboeducattain.Text,dtpdate.Text,txtfather1.Text,txtfather2.Text,txtfather3.Text,txtmother1.Text,
                txtmother2.Text,txtmother3.Text,spouse,txtspouse4.Text,txtboardexam.Text,txtcivilservice.Text,txtdriverslicense.Text,
                txtprcnumber.Text,txttesda.Text,txtothers.Text,txtsurname.Text);
            
        }
        //end of adding employee records

        //adding qualifications
        void addqual_addcompleted(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                EventHandler eh = new EventHandler(addqual_addcompleted);
                this.Invoke(eh);
                return;
            }
            addqual.donCommit();            
            
        }
        void btnaddcareer_Click(object sender, EventArgs e)
        {
            if (checkcareer1.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer1.Text);
            }
            if (checkcareer2.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer2.Text);
            }
            if (checkcareer3.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer3.Text);
            }
            if (checkcareer4.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer4.Text);
            }
            if (checkcareer5.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer5.Text);
            }
            if (checkcareer6.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer6.Text);
            }
            if (checkcareer7.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer7.Text);
            }
            if (checkcareer8.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer8.Text);
            }
            if (checkcareer9.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer9.Text);
            }
            if (checkcareer10.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer10.Text);
            }
            if (checkcareer11.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer11.Text);
            }
            if (checkcareer12.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer12.Text);
            }
            if (checkcareer13.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer13.Text);
            }
            if (checkcareer14.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer14.Text);
            }
            if (checkcareer15.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer15.Text);
            }
            if (checkcareer16.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer16.Text);
            }
            if (txtcareer1.Text != "" && txtcareer1.Text != "Others")
            {
                addqual.asyncaddqualification(txtempnum.Text, txtcareer1.Text);
            }
            if (txtcareer2.Text != "" && txtcareer2.Text != "Others")
            {
                addqual.asyncaddqualification(txtempnum.Text, txtcareer2.Text);
            }
            MessageBox.Show("Qualifications Successfully Added!");
            setcareertext();
        }
        //end of adding qualifications

        //adding dependents
        void adddeps_addcompleted(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                EventHandler eh = new EventHandler(adddeps_addcompleted);
                this.Invoke(eh);
                return;
            }
            adddeps.donCommit();

            MessageBox.Show("Dependents Successfully Added!");
            setdependentstext();
        }
        void btnadddependents_Click(object sender, EventArgs e)
        {
            string depname = txtchild1b.Text + " " + txtchild1c.Text + ". " + txtchild1a.Text;
            adddeps.asyncadddependents(txtempnum.Text, depname, txtchild1d.Text, txtchild1e.Text, txtchild1f.Text);  
        }
        //end of adding dependents


        void btnlogout_Click(object sender, EventArgs e)
        {
            formLogin = new LoginForm();
            this.Hide();
            formLogin.Show();
        }

        void UserForm_Load(object sender, EventArgs e)
        {
            adddeps.dbConnect();
            adddeps.dbQuery("SELECT * FROM tblDependents");
            addqual.dbConnect();
            addqual.dbQuery("SELECT * FROM tblQualification");
            ae.dbConnect();
            ae.dbQuery("SELECT * FROM tblProfiles");

            if (!camera.IsConnected())
            {
                camera.Connect();
                
                backgroundWorker1.RunWorkerAsync();
            }
            else
            {

                backgroundWorker1.CancelAsync();

            }
           
        }
        

        
        /*void btnaddcareer_Click(object sender, EventArgs e)
        {
            string career = "";
            if (checkcareer1.Checked == true)
            {
                career = checkcareer1.Text;
            }
        }

        void btnsave_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Records Successfully Saved!");
            settext();
            
        }

        void btnaddemployee_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Children added to beneficiaries.");
            settext();
        }

        void btnlogout_Click(object sender, EventArgs e)
        {
            formLogin = new LoginForm();
            this.Hide();
            formLogin.Show();
        }*/

        void settext()
        {
            txtempnum.Text = "";
            combostatus.Text = "Status";
            txtsurname.Text = "Surname";
            txtfirstname.Text = "First Name";
            txtmiddlename.Text = "Middle Name";
            txtgender.Text = "Gender";
            txtempcontact.Text = "CP/LANDLINE";
            txtpresadd1.Text = "No.";
            txtpresadd2.Text = "Street";
            txtpresadd3.Text = "City";
            txtpresadd4.Text = "Province";
            txtpresadd5.Text = "Tel. No.";
            txtpermadd1.Text = "No.";
            txtpermadd2.Text = "Street";
            txtpermadd3.Text = "City";
            txtpermadd4.Text = "Province";
            txtpermadd5.Text = "Tel. No.";
            txtempheight.Text = "Height";
            txtempweight.Text = "Weight";
            radbtnsingle.Checked = false;
            radbtnmarried.Checked = false;
            radbtnwidow.Checked = false;
            radbtnseparated.Checked = false;
            combomonth.Text = "Month";
            comboyear.Text = "Year";
            comboday.Text = "Day";
            txtage.Text = "Age";
            txtfather1.Text = "Father's Name";
            txtfather2.Text = "Address";
            txtfather3.Text = "Tel. No.";
            txtmother1.Text = "Mother's Name";
            txtmother2.Text = "Address";
            txtmother3.Text = "Tel. No.";
            txtspouse1.Text = "LastName";
            txtspouse2.Text = "FirstName";
            txtspouse3.Text = "MiddleName";
            txtspouse4.Text = "Occupation";
            
            comboeducattain.Text = "Highest Educational Attainment";
            txtboardexam.Text = "Board Exams Rating";
            txtcivilservice.Text = "CSC Eligibility & Rating";
            txtdriverslicense.Text = "Driver's License and Restriction";
            txtprcnumber.Text = "PRC Number";
            txttesda.Text = "Tesda NC Title & Cert. No.";
            txtothers.Text = "Others";
            txtdept.Text = "Department Assigned";
            txtpos.Text = "Position";
            txttin.Text = "TIN";
            txtsss.Text = "SSS";
            txtphilhealth.Text = "PHILHEALTH";
            txtpagibig.Text = "PAGIBIG";
             
        }
        void setcareertext()
        {
            checkcareer1.Checked = false;
            checkcareer2.Checked = false;
            checkcareer3.Checked = false;
            checkcareer4.Checked = false;
            checkcareer5.Checked = false;
            checkcareer6.Checked = false;
            checkcareer7.Checked = false;
            checkcareer8.Checked = false;
            checkcareer9.Checked = false;
            checkcareer10.Checked = false;
            checkcareer11.Checked = false;
            checkcareer12.Checked = false;
            checkcareer13.Checked = false;
            checkcareer14.Checked = false;
            checkcareer15.Checked = false;
            checkcareer16.Checked = false;
            txtcareer1.Text = "Others";
            txtcareer2.Text = "Others";
        }
        void setdependentstext()
        {
            txtchild1a.Text = "LastName";
            txtchild1b.Text = "FirstName";
            txtchild1c.Text = "MiddleName";
            txtchild1d.Text = "Age";
            txtchild1e.Text = "Sex";
            txtchild1f.Text = "Birthdate";
        }

    }
}
